<?php
namespace kykkk;
use think\template\TagLib;
class Ky extends TagLib{
	protected $tags   =  [
	        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
	        'prev'      => ['attr' => 'class,nomore', 'close' => 0], //闭合标签，默认为不闭合
	        'next'      => ['attr' => 'class,nomore', 'close' => 0], //闭合标签，默认为不闭合
	        'position'  => ['attr' => '', 'close' => 0], //闭合标签，默认为不闭合
	        'list'      => ['attr' => 'row,infolen,orderby'],
	        'goodslist' => ['attr' => 'name,id,offset,length,key,mod'],
	        'ad'     	=> ['attr' => 'id'],
	        'arclist'   => ['attr' => 'row,navid,flag,orderby,titlelen,infolen,calid'],
	        'garclist'  => ['attr' => 'row,navid,flag,orderby,titlelen,infolen'],
	        'article'   => ['attr' => 'row,flag,orderby,titlelen,infolen'],
	        'navlist'   => ['attr' => 'type,row,id'],
	        'gnavlist'  => ['attr' => 'type,row,id'],
	        'banner'    => ['attr' => 'platform,web,orderby'],
	        'custom'    => ['attr' => 'type,row'],
	        'brand'     => ['attr' => 'orderby,row'],
	        'link'      => ['attr' => 'type,orderby'],
	        'navtype'   => ['attr' => 'id'],
	        'gnavtype'   => ['attr' => 'id'],
	];
	public function tagAbc($tag)
    {
        $parse = '<?php ';
        $parse .= 'echo "aaaa";';
        $parse .= '?>';
        return $parse;
    }
public function tagPrev($tag){
	$class = isset($tag['class']) ? $tag['class'] : '';
	$nomore = isset($tag['nomore']) ? $tag['nomore'] : '暂无更多';
$str=<<<EOF
<?php
	\$alllid = \$navt::getAllId(\$orgnav,\$navself['id']);
	\$prev   = \\think\Db::name(\$navself['table'])
				->where('navid','eq',\$cont['navid'])
				->where('id','lt',\$cont['id'])
				->order('id desc')
				->find();
	if(\$prev){
        echo "<a class='$class' href='/content_".\$cont['navid']."_".\$prev['id']."'>".\$prev['title']."</a>";
      }else{
        echo "<a class='$class' href='javascript::void(0)'>$nomore</a>";
    }
?>
EOF;
return $str;
}
public function tagNext($tag){
	$class = isset($tag['class']) ? $tag['class'] : '';
	$nomore = isset($tag['nomore']) ? $tag['nomore'] : '暂无更多';
$str=<<<EOF
<?php

	\$alllid = \$navt::getAllId(\$orgnav,\$navself['id']);
	\$prev   = \\think\Db::name(\$navself['table'])
				->where('navid','eq',\$cont['navid'])
				->where('id','gt',\$cont['id'])
				->order('id asc')
				->find();
	if(\$prev){
        echo "<a class='$class' href='/content_".\$cont['navid']."_".\$prev['id']."'>".\$prev['title']."</a>";
      }else{
        echo "<a class='$class' href='javascript::void(0)'>$nomore</a>";
    }
?>
EOF;
return $str;
}

public function tagPosition($tag){
$parse = <<<EOF
<?php
	
	\$str = "<a href='/'>".\$indexname."</a>";
    foreach(\$position as \$nav){
    	if(\$mname == 'index'){
    		\$str.="<a href='/category_".\$nav['id'].".html'>".\$nav['navname']."</a>";
    	}else{
    		\$str.="<a href='/goods_".\$nav['id'].".html'>".\$nav['navname']."</a>";
    	}
    }
	echo \$str;
?>
EOF;
return $parse;
}

public function tagList($tag, $content){
	$row   	  = isset($tag['row']) ? $tag['row'] : '1';
	$infolen  = isset($tag['infolen']) ? $tag['infolen'] : '65';
	$orderby  = isset($tag['orderby']) ? $tag['orderby'] : 'rank desc';
$str = <<<EOF
<?php
	\$page 	  = isset(\$_GET['page']) ? \$_GET['page'] : '1';
	\$allid = \$navt::getAllId(\$orgnav,\$navself['id']);
	\$table = \$navself['table'];
	if(\$hasauth == 'n'){
		echo \$navself['content'];
		\$ret['datalist'] = [];
	}else{
		\$ret = get_list('home',"\$table",LID,"\$allid","$row","$orderby",'1',"\$page",\$navself['id']);
	}
	
	foreach(\$ret['datalist'] as \$arc):
		\$arc['description'] = mb_substr(\$arc['description'], 0,$infolen,'UTF-8');
		if(mb_strlen(\$arc['jump'])>5){
			\$arc['url'] = \$arc['jump'];
		}else{
			\$arc['url'] = '/content_'.\$arc['navid'].'_'.\$arc['id'].'.html';
		}
	\$pagelist = \$ret['pagelist'];
?>
EOF;
	$str.= $content;
  	$str.='<?php endforeach;?>';
	return $str;
}


public function tagGoodslist($tag, $content){
	$row   	  = isset($tag['row']) ? $tag['row'] : '1';
	$orderby  = isset($tag['orderby']) ? $tag['orderby'] : 'rank desc';

$str = <<<EOF
<?php
	\$page 	  = isset(\$_GET['page']) ? \$_GET['page'] : '1';
	\$allid   = \$navt::getAllId(\$goodsorgnav,\$navself['id']);
	
	\$ret = get_goodslist('home',LID,"\$allid","$row","$orderby",'1',"\$page","\$spectj","\$impact");

	foreach(\$ret['datalist'] as \$arc):
		if(mb_strlen(\$arc['jump'])>5){
			\$arc['url'] = \$arc['jump'];
		}else{
			\$arc['url'] = '/goods_'.\$arc['navid'].'_'.\$arc['id'].'.html';
		}
	\$pagelist = \$ret['pagelist'];
?>
EOF;
	$str.= $content;
  	$str.='<?php endforeach;?>';
	return $str;
}


//内容辅助器
public function tagAd($tag,$content){
	$id = $tag['id'];
$str = <<<EOF
<?php
	\$ad = getone(config('database.prefix').'plugad',['id'=>$id],true);
	\$ad['content'] = htmlspecialchars_decode(\$ad['content']);
?>
EOF;
  $str.= $content;
  return $str;
}

public function tagArclist($tag,$content){
	$row 		= isset($tag['row']) ? $tag['row'] : '10';
	$navid		= isset($tag['navid']) ? $tag['navid'] : '0';
	$cid		= isset($tag['cid']) ? $tag['cid'] : '0';
	$flag		= isset($tag['flag']) ? $tag['flag'] : '';
	$orderby	= isset($tag['orderby']) ? $tag['orderby'] : 'savedate desc';
	$titlelen	= isset($tag['titlelen']) ? $tag['titlelen'] : '60';
	$infolen	= isset($tag['infolen']) ? $tag['infolen'] : '80';
$str = <<<EOF
<?php
	if (intval($cid) == 0 && intval($navid) == 0) {
		exit('cid and navid must be set one of them');
	}
	if(intval($cid)>0){
		\$curcal = \$navt::get_cal(\$calarr,$cid);
		\$data = get_arclist(\$curcal['table'],LID,"",$row,"$flag","$orderby",$navid);
	}else{
		\$allid = \$navt::getAllId(\$orgnav,$navid);
		\$nav = \$navt::getSelf(\$orgnav,$navid);
		\$data = get_arclist(\$nav['table'],LID,"\$allid",$row,"$flag","$orderby",$navid);
	}
	

	foreach(\$data as \$auto=>\$arc):
	\$arc['fulltitle'] = \$arc['title'];
	\$arc['title'] = mb_substr(\$arc['title'], 0,$titlelen,'UTF-8');
	\$arc['description'] = mb_substr(\$arc['description'], 0,$infolen,'UTF-8');
	\$arc['url'] = '/content_'.\$arc['navid'].'_'.\$arc['id'].'.html';
?>
EOF;
  $str.= $content;
  $str.='<?php endforeach;?>';
  return $str;
}

public function tagGarclist($tag,$content){
	$row 		= isset($tag['row']) ? $tag['row'] : '10';
	$navid		= isset($tag['navid']) ? $tag['navid'] : '0';
	$flag		= isset($tag['flag']) ? $tag['flag'] : '';
	$orderby	= isset($tag['orderby']) ? $tag['orderby'] : 'rank desc';
	$titlelen	= isset($tag['titlelen']) ? $tag['titlelen'] : '60';
	$infolen	= isset($tag['infolen']) ? $tag['infolen'] : '80';
$str = <<<EOF
<?php
	\$allid = \$navt::getAllId(\$goodsorgnav,$navid);
	\$data = get_garclist($navid,LID,"\$allid",$row,"$flag","$orderby");
	foreach(\$data as \$auto=>\$arc):
	\$arc['url'] = '/goods_'.\$arc['navid'].'_'.\$arc['id'].'.html';
?>
EOF;
  $str.= $content;
  $str.='<?php endforeach;?>';
  return $str;
}


//navlist:导航列表标签
public function tagNavlist($tag,$content){
	$type 		= isset($tag['type']) ? $tag['type'] : 'top';
	$row 		= isset($tag['row']) ? $tag['row'] : '30';
	$id 		= isset($tag['id']) ? $tag['id'] : '0';
$str =<<<EOF
<?php

	switch("$type"){
		case 'top':
		\$navdata = \$navlist;
		break;
		case 'son':
		\$navdata = \$navt::getNavList(\$orgnav,$id);
		break;
	}
	\$auto = 0;
	
	foreach(\$navdata as \$nav):
		if(\$auto++ >= $row){
			continue;
		}
		
		\$nav['url'] = '/category_'.\$nav['id'].'.html';
?>
EOF;
  $str.= $content;
  $str.='<?php endforeach;?>';
  return $str;
}


//gnavlist:商城导航标签
public function tagGnavlist($tag,$content){
	$type 		= isset($tag['type']) ? $tag['type'] : 'top';
	$row 		= isset($tag['row']) ? $tag['row'] : '30';
	$id 		= isset($tag['id']) ? $tag['id'] : '0';
$str =<<<EOF
<?php
	
	\$navdata = \$navt::getSon(\$goodsorgnav,$id);
	\$auto = 0;
	foreach(\$navdata as \$nav):
		if(\$auto++ >= $row){
			continue;
		}
		\$nav['url'] = \$nav['jump'] ? \$nav['jump'] : \$nav['id'];
		\$nav['url'] = '/goods_'.\$nav['url'].'.html';
?>
EOF;
  $str.= $content;
  $str.='<?php endforeach;?>';
  return $str;
}

public function tagBanner($tag,$content){
	$platform =  isset($tag['platform']) ? $tag['platform'] : 'p';
	$web =  isset($tag['web']) ? $tag['web'] : 'c';
	$orderby =  isset($tag['orderby']) ? $tag['orderby'] : 'rank desc';
	$str = <<<EOF
<?php
	\$bannerw['lid'] = ['eq',LID];
	\$bannerw['platform'] = ['eq',$platform];
	\$bannerw['cors'] = ['eq',$web];
	foreach(getall(\$prefix.'plugbanner',\$bannerw,'$orderby',TRUE) as \$banner):
?>
EOF;
$str.= $content;
$str.='<?php endforeach;?>';
return $str;
}

public function tagCustom($tag,$content){
	$type = $tag['type'];
	$row =  isset($tag['row']) ? $tag['row'] : '100';
	$orderby =  isset($tag['orderby']) ? $tag['orderby'] : 'rank ASC';
	$str = <<<EOF
<?php
	\$customdata = get_custom('home',LID,"$type","$row","$orderby");

	foreach(\$customdata['datalist'] as \$custom):
?>
EOF;
$str.= $content;
$str.='<?php endforeach;?>';
return $str;
}


public function tagBrand($tag,$content){
	$orderby =  isset($tag['orderby']) ? $tag['orderby'] : 'id desc';
	$row 	 =  isset($tag['row']) ? $tag['row'] : '15';
	$str = <<<EOF
<?php

	foreach(get_brand('home',"$orderby","$row") as \$brand):

?>
EOF;
$str.= $content;
$str.='<?php endforeach;?>';
return $str;
}

public function tagLink($tag,$content){
	$type 		= isset($tag['type']) ? $tag['type'] : 't';
	$orderby    = isset($tag['orderby']) ? $tag['orderby'] : 'rank desc';
	$str = <<<EOF
<?php
	\$linkw['lid']  = ['eq',LID];
	\$linkw['linktype'] = ['eq',$type]; 
	
	foreach(getall(\$prefix.'pluglink',\$linkw,"$orderby",TRUE) as \$link):

?>
EOF;
$str.= $content;
$str.='<?php endforeach;?>';
return $str;
}



public function tagNavtype($tag,$content){
	$id = $tag['id'];
	$str = <<<EOF
<?php
	\$nav = \$navt::getSelf(\$orgnav,$id);
	\$nav['url'] = '/category_'.\$nav['id'].'.html';
?>
EOF;
$str.= $content;
return $str;
}

public function tagGnavtype($tag,$content){
	$id = $tag['id'];
	$str = <<<EOF
<?php
	\$nav = \$navt::getSelf(\$goodsorgnav,$id);
	\$nav['url'] = '/category_'.\$nav['id'].'.html';
?>
EOF;
$str.= $content;
return $str;
}


}